package cn.edu.neusoft.food729.model;


import cn.edu.neusoft.food729.Bean.RegisterBean;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.iface.updateUserByIdiface;
import cn.edu.neusoft.food729.listener.RegisterListener;
import cn.edu.neusoft.food729.service.updateUserByIdService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class updateUserByIdModel implements updateUserByIdiface {

    private Retrofit retrofit;

    public updateUserByIdModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getRegisterResult(String id, String username, String userpass, String mobilenum, String adress, final RegisterListener registerListener) {
//        RegisterService registerService
//                = retrofit.create(RegisterService.class);
        updateUserByIdService updateUserByIdService = retrofit.create(cn.edu.neusoft.food729.service.updateUserByIdService.class);
        Call<RegisterBean> call = updateUserByIdService.update(id, username, userpass, mobilenum, adress);

        call.enqueue(new Callback<RegisterBean>() {
            @Override
            public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                String s1 = response.body().getSuccess();
                String s2 = "0";
                if (response.isSuccessful() && (!s1.equals(s2))) {
                    registerListener.onResponse(response.body());
                } else if ((s1.equals(s2)))
                    registerListener.onFail("修改失败");
                else registerListener.onFail("未知错误");
//            }
            }

            @Override
            public void onFailure(Call<RegisterBean> call, Throwable t) {
                registerListener.onFail(t.getMessage());
                System.out.println("--网络错误：" + t.getMessage());
            }
        });
    }
}
